package com.wanghaicheng.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;

public class FileUtil {
    public static ByteArrayOutputStream getFile(String absolutePath) {
        try {
            String[] split = absolutePath.split("\\.");
            String fileType = split[split.length - 1];
            //根据后缀,创建工作簿
            if (!"jpg".equals(fileType) && !"png".equals(fileType)) {
                throw new RuntimeException("文件格式不支持");
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BufferedImage bufferImg = ImageIO.read(new File(absolutePath));
            ImageIO.write(bufferImg, fileType, baos);
            return baos;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("读取文件失败");
        }
    }
}
